package com.sunny.myproject.client.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sunny.myproject.core.base.action.BaseAction;
import com.sunny.myproject.client.entity.Client;
import com.sunny.myproject.client.service.ClientService;

@Controller
@RequestMapping("client/client")
public class ClientAction extends BaseAction<Client>{

	private String title = "客户";
	private String action = "client/client";
	private String function = "client";

	private Logger logger = Logger.getLogger(ClientAction.class);
	
	
	@Autowired
	private ClientService service;
	
	@Override
	@RequestMapping(params="list")
	public String list(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			super.pager = super.pageStart(model, request, response, action, function, title);
			super.pager = super.pageEnd(model, service.num(criterions), pager);
			super.returnValue(model, service.find(criterions, pager));
		} catch (Exception e) {
			logger.error(e);
		}
		return "client/clientList";
	}

	@Override
	@RequestMapping(params="add")
	public String add(HttpServletRequest request, HttpServletResponse response) {
		super.add(title, request);
		return "client/clientEdit";
	}

	@Override
	@RequestMapping(params="edit")
	public String edit(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			super.returnValue(model, service.findById(super.edit(title, model, request)));
		} catch (Exception e) {
			logger.error(e);
		}
		return "client/clientEdit";
	}

	@Override
	@RequestMapping(params="delete")
	public String delete(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			service.deleteByIds(super.param2(request));
		} catch (Exception e) {
			logger.error(e);
		}
		return "redirect:/client/client?list";
	}

	@Override
	@RequestMapping(params="save")
	public String save(HttpServletRequest request,
			HttpServletResponse response,@ModelAttribute Client t) {
		try {
			service.save(t);
		} catch (Exception e) {
			logger.error(e);
		}
		return "redirect:/client/client?list";
	}

	@Override
	@RequestMapping(params="view")
	public String view(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			super.returnViewValue(model, service.findById(super.view(title, model, request)));
		} catch (Exception e) {
			logger.error(e);
		}
		return "client/clientEdit";
	}
	
	@RequestMapping(params="query")
	public String query(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			super.pager = super.pageStart(model, request, response, action, function, title);
			super.pager = super.pageEnd(model, service.num(criterions), pager);
			super.returnValue(model, service.find(criterions, pager));
		} catch (Exception e) {
			logger.error(e);
		}
		return "client/clientQuery";
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		//注册定义的属性编辑器
		//1.日期
		 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		 CustomDateEditor dateEditor = new CustomDateEditor(df, true);
		 //表示如果命令对象有date类型的属性,将使用该属性编辑器进行类型转换
		 binder.registerCustomEditor(Date.class, dateEditor);
	}
}
